package lj.courseinterect.rest.resources;

import lj.courseinterect.dao.i.CourseInfoDao;
import lj.courseinterect.dao.i.StudentCourseDao;
import lj.courseinterect.dao.i.StudentDao;
import lj.courseinterect.dao.i.StudentItemDao;
import lj.courseinterect.entity.CourseInfo;
import lj.courseinterect.entity.Student;
import lj.courseinterect.entity.StudentCourse;
import lj.courseinterect.entity.StudentItem;
import lj.courseinterect.transfer.OperationTransfer;
import lj.courseinterect.transfer.StudentCourseTransfer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;


@Component
@Path("/{a:tea|stu}/student_course")
public class StudentCourseResource
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StudentCourseDao studentCourseDao;

    @Autowired
    private StudentResource studentResource;

    @Autowired
    private CourseInfoDao courseInfoDao;

    @Autowired
    private StudentDao studentDao;

    public Logger getLogger() {
        return logger;
    }

    public StudentCourseDao getStudentCourseDao() {
        return studentCourseDao;
    }

    public void setStudentCourseDao(StudentCourseDao studentCourseDao) {
        this.studentCourseDao = studentCourseDao;
    }

//    @POST
//    @Produces(MediaType.APPLICATION_JSON)
//    @Consumes(MediaType.APPLICATION_JSON)
//    public OperationTransfer create(StudentCourse studentCourse)
//    {
//        this.logger.info("create(): " + studentCourse.getLocCount());
//        OperationTransfer operationTransfer;
//
//        try {
//            this.studentCourseDao.save(studentCourse);
//            operationTransfer = new OperationTransfer(true, "选课成功");
//        } catch (Exception ex){
//            operationTransfer = new OperationTransfer(false, "选课失败");
//        }
//
//        this.logger.info("create(): " + operationTransfer.toString());
//        return operationTransfer;
//    }

    /**
     * 添加课程
     * @param couId
     * @return
     */
    @Path("addcou")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public OperationTransfer addCourse(@FormParam("couId") Long couId){
        Student student = studentResource.getStudent();

        CourseInfo courseInfo = courseInfoDao.find(couId);
        if(courseInfo != null){
            // 课程存在, 判断是否已经选择
            StudentCourse studentCourse = studentCourseDao.findByStuIdAndCouId(student.getStuId(), couId);
            if(studentCourse != null){
                // 已经选课
                return new OperationTransfer(false, "已经选课");
            } else {
                studentCourse = new StudentCourse();
                studentCourse.setCouId(couId);
                studentCourse.setStuId((student.getStuId()));

                try{
                    studentCourseDao.save(studentCourse);
                    courseInfo.setCouCount(courseInfo.getCouCount() + 1);
                    courseInfoDao.save(courseInfo);
                    return new OperationTransfer(true, "选课成功");
                } catch (Exception ex){
                    return new OperationTransfer(false, "选课失败");
                }
            }
        } else {
            return new OperationTransfer(false, "课程不存在");
        }
    }

    /**
     * 获得学生已选课程
     * @return
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<StudentCourse> getCouByStuId(){
        return studentCourseDao.findByStuId(studentResource.getStudent().getStuId());
    }

    @GET
    @Path("getCourseStudents/{couId}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<StudentCourseTransfer> getCourseStudents(@PathParam("couId") Long couId){
        List<StudentCourse> studentCourses = studentCourseDao.findByCouId(couId);
        List<StudentCourseTransfer> studentCourseTransfers = new ArrayList<StudentCourseTransfer>();

        for(StudentCourse studentCourse : studentCourses){
            Student student = studentDao.find(studentCourse.getStuId());

            StudentCourseTransfer studentCourseTransfer = new StudentCourseTransfer();
            studentCourseTransfer.setStudent(student);
            studentCourseTransfer.setStudentCourse(studentCourse);

            studentCourseTransfers.add(studentCourseTransfer);
        }

        return studentCourseTransfers;
    }

    /**
     * 获得学生单个已选课程
     * @return
     */
    @GET
    @Path("get/{scId: [0-9]*$}")
    @Produces(MediaType.APPLICATION_JSON)
    public StudentCourse getStudentCourseById(@PathParam("scId")Long scId){
        return studentCourseDao.find(scId);
    }
}